<?php
namespace app\index\controller;

use app\admin\model\Article;
use app\admin\model\Category;
use app\admin\model\Lrtrain;
use app\admin\model\User;
use tool\Csv;
use think\Controller;

class Index extends Controller
{



    //推荐控制器
    public function index(){
        $send = input();

        $ll_cid = empty($send['ll_cid'])? 1 : $send['ll_cid'] ;
        $catmodel = new  Category();
        $cattree = $catmodel->getAllCategory();//目录信息
        $catnottree = $catmodel->getCatNotTree($cattree);//分类变成一维
        $articlemodel = new Article();

        if (session('slogin')){

            //更新csv文件
//            $rating = new \app\admin\model\Rating();
//            $rating->csv();

            //如果登录了，显示推荐的内容
            $list = curl_request('http://localhost:8000/?uid='.session('ll_suid'));
            $list = json_decode($list,true);

            if(empty($list[session('ll_suid')])){
                $articlearr = $articlemodel->getNewslistMoreMsg($ll_cid);//新闻列表数据
            }else{

                //推荐列表

                $aidslist = $list[session('ll_suid')];
                $aids = array_column($aidslist,0);
                $token=md5(session('ll_suid').time());
                $this->assign('token',$token);
                cache($token,$aids);
                $lrmodel = new Lrtrain();
                $lrmodel->addRecitem(session('ll_suid'),$aids,$token);//添加lr模型的推荐记录
                $recarticlearr = $articlemodel->getNewsRecdMoreMsg($aids);//推荐新闻列表数据
            }


        }else{
            //不登录显示全部
            $articlearr = $articlemodel->getNewslistMoreMsg($ll_cid);//新闻列表数据
        }


        $this->assign('catnt',$catnottree);
        $this->assign('ll_cid',$ll_cid);
        if (!empty($recarticlearr)){
            $this->assign('recarticlearr',$recarticlearr);
        }else{
            $this->assign('articlearr',$articlearr);
        }
        $this->assign('type','cf');

        return $this->fetch();
    }

    //lfm
    public function lfm(){
        $send = input();

        $ll_cid = empty($send['ll_cid'])? 1 : $send['ll_cid'] ;
        $catmodel = new  Category();
        $cattree = $catmodel->getAllCategory();//目录信息
        $catnottree = $catmodel->getCatNotTree($cattree);//分类变成一维
        $articlemodel = new Article();

        if (session('slogin')){

            //更新csv文件
//            $rating = new \app\admin\model\Rating();
//            $rating->csv();

            //如果登录了，显示推荐的内容
            $list = curl_request('http://localhost:8001/?uid='.session('ll_suid'));
//            dump($list);
            $list = json_decode($list,true);

            if(empty($list[session('ll_suid')])){
                $articlearr = $articlemodel->getNewslistMoreMsg($ll_cid);//新闻列表数据
            }else{

                //推荐列表

                $aidslist = $list[session('ll_suid')];
                $aids = array_column($aidslist,0);
                $token=md5(session('ll_suid').time());
                $this->assign('token',$token);
                cache($token,$aids);
                $lrmodel = new Lrtrain();
                $lrmodel->addRecitem(session('ll_suid'),$aids,$token);//添加lr模型的推荐记录
                $recarticlearr = $articlemodel->getNewsRecdMoreMsg($aids);//推荐新闻列表数据
            }


        }else{
            //不登录显示全部
            $articlearr = $articlemodel->getNewslistMoreMsg($ll_cid);//新闻列表数据
        }


        $this->assign('catnt',$catnottree);
        $this->assign('ll_cid',$ll_cid);
        if (!empty($recarticlearr)){
            $this->assign('recarticlearr',$recarticlearr);
        }else{
            $this->assign('articlearr',$articlearr);
        }
        $this->assign('type','lfm');
        return $this->fetch();
    }

    //person_rank
    public function pr(){
        $send = input();

        $ll_cid = empty($send['ll_cid'])? 1 : $send['ll_cid'] ;
        $catmodel = new  Category();
        $cattree = $catmodel->getAllCategory();//目录信息
        $catnottree = $catmodel->getCatNotTree($cattree);//分类变成一维
        $articlemodel = new Article();

        if (session('slogin')){

            //更新csv文件
//            $rating = new \app\admin\model\Rating();
//            $rating->csv();

            //如果登录了，显示推荐的内容
            $list = curl_request('http://localhost:8004/?uid='.session('ll_suid'));
//            dump($list);
            $list = json_decode($list,true);

            if(empty($list[session('ll_suid')])){
                $articlearr = $articlemodel->getNewslistMoreMsg($ll_cid);//新闻列表数据
            }else{

                //推荐列表

                $aidslist = $list[session('ll_suid')];
                $aids = array_column($aidslist,0);
                $token=md5(session('ll_suid').time());
                $this->assign('token',$token);
                cache($token,$aids);
                $lrmodel = new Lrtrain();
                $lrmodel->addRecitem(session('ll_suid'),$aids,$token);//添加lr模型的推荐记录
                $recarticlearr = $articlemodel->getNewsRecdMoreMsg($aids);//推荐新闻列表数据
            }


        }else{
            //不登录显示全部
            $articlearr = $articlemodel->getNewslistMoreMsg($ll_cid);//新闻列表数据
        }


        $this->assign('catnt',$catnottree);
        $this->assign('ll_cid',$ll_cid);
        if (!empty($recarticlearr)){
            $this->assign('recarticlearr',$recarticlearr);
        }else{
            $this->assign('articlearr',$articlearr);
        }
        $this->assign('type','pr');

        return $this->fetch();
    }

    //item2vec
    public function item2vec(){
        $send = input();

        $ll_cid = empty($send['ll_cid'])? 1 : $send['ll_cid'] ;
        $catmodel = new  Category();
        $cattree = $catmodel->getAllCategory();//目录信息
        $catnottree = $catmodel->getCatNotTree($cattree);//分类变成一维
        $articlemodel = new Article();

        if (session('slogin')){

            //更新csv文件
//            $rating = new \app\admin\model\Rating();
//            $rating->csv();

            //如果登录了，显示推荐的内容
            $list = curl_request('http://localhost:8005/?uid='.session('ll_suid'));
//            dump($list);
            $list = json_decode($list,true);

            if(empty($list[session('ll_suid')])){
                $articlearr = $articlemodel->getNewslistMoreMsg($ll_cid);//新闻列表数据
            }else{

                //推荐列表

                $aidslist = $list[session('ll_suid')];
                $aids = array_column($aidslist,0);
                $token=md5(session('ll_suid').time());
                $this->assign('token',$token);
                cache($token,$aids);
                $lrmodel = new Lrtrain();
                $lrmodel->addRecitem(session('ll_suid'),$aids,$token);//添加lr模型的推荐记录
                $recarticlearr = $articlemodel->getNewsRecdMoreMsg($aids);//推荐新闻列表数据
            }


        }else{
            //不登录显示全部
            $articlearr = $articlemodel->getNewslistMoreMsg($ll_cid);//新闻列表数据
        }


        $this->assign('catnt',$catnottree);
        $this->assign('ll_cid',$ll_cid);
        if (!empty($recarticlearr)){
            $this->assign('recarticlearr',$recarticlearr);
        }else{
            $this->assign('articlearr',$articlearr);
        }
        $this->assign('type','item2vec');

        return $this->fetch();
    }


    public function listjson(){
        $send = input();
        $page = $send['page'];
        $ll_cid = empty($send['ll_cid'])? 1 : $send['ll_cid'] ;
        $articlemodel = new Article();
        $articlearr = $articlemodel->getNewslistMoreMsg($ll_cid,$page);//新闻列表数据


//        echo  $articlemodel->getLastSql();
        echo json_encode([
            'code'=>1,
            'msg'=>'列表',
            'list'=>$articlearr
        ]);

    }




    //前台首页
    public function getlist(){

        $send = input();
        $ll_cid = empty($send['ll_cid'])? 1 : $send['ll_cid'] ;
        $catmodel = new  Category();
        $cattree = $catmodel->getAllCategory();//目录信息
        $catnottree = $catmodel->getCatNotTree($cattree);//分类变成一维

        $articlemodel = new Article();
        $articlearr = $articlemodel->getNewslistMoreMsg($ll_cid);//新闻列表数据
        

        $this->assign('catnt',$catnottree);
        $this->assign('ll_cid',$ll_cid);
        $this->assign('articlearr',$articlearr);
        $this->assign('type','');

        return $this->fetch('index/index');
    }

    /**
     * 导出csv
     *
     *
     * **/
    public function csv(){
        $csv = new Csv();
        $ratingmodel = new \app\admin\model\Rating();
        $list =  $ratingmodel->field('ll_uid,ll_aid,ll_rating,ll_time')->select();
        $list = collection($list)->toArray();
        $csv_title = array('userId','AID','rating','timestamp');
        $csv->put_csv($list,$csv_title);
    }



    public function addUser(){
        $usermodel = new User();
        for ($i=17;$i<100;$i++){
            $data=[
                'll_password'=>'e10adc3949ba59abbe56e057f20f883e',
                'll_username'=>'lili'.$i,
                'll_type'=>2,
                'll_uid'=>$i
            ];
            $usermodel->isUpdate(false)->save($data);
        }
    }


    public function addRating(){
        set_time_limit(0);
        $usermodel = new User();
        $uids = array_column(collection($usermodel->field('ll_uid')->select())->toArray(),'ll_uid') ;
//        dump(($uids));
        $articlemodel = new Article();
        $aids = array_column(collection($articlemodel->field('ll_aid')->select())->toArray(),'ll_aid') ;
//        dump(($aids));
        for ($i=0;$i<100000;$i++){

            $uidindex = rand(0,count($uids)-1);
            $aidindex = rand(0,count($aids)-1);
            $rating = new \app\admin\model\Rating();
            $rating->add( $uids[$uidindex] , $aids[$aidindex] ,'click');
        }

    }

}
